package org.vectrics.recruiter.struts.tag;

import java.util.Properties;

import javax.servlet.ServletRequest;

import org.apache.log4j.Category;
import org.vectrics.user.Person;
import org.vectrics.user.UserGroup;
import org.vectrics.user.web.struts.UserEditTag;

public abstract class AdminUserEditTag extends UserEditTag {

	private static final long serialVersionUID = 5365854794081729188L;
	
	static private Category log = Category.getInstance(AdminUserEditTag.class);
    private Properties roles = new Properties();
    private boolean showPassword = true;
	private boolean showRoles = true;
	private boolean showUserGroups = true;
	private String contextPath = null;
    
    public AdminUserEditTag() {
    	super();
    	log.debug("constructor");
		roles.setProperty("recruit-mgr", "Recruiting User");
		roles.setProperty("recruit-recruiter", "Recruiting User");
		roles.setProperty("recruit-candidate", "Recruiting Candidate");
		roles.setProperty("recruit-employee", "Recruiting Staff");
		roles.setProperty("recruit-admin", "Recruiting Administrator");
    }

	protected boolean getShowPrefix(ServletRequest request) {
		return(true);
	}

	protected boolean getShowGender(ServletRequest request) {
		return(true);
	}

	protected boolean getAllowUserNameEdit() {
		return(true);	
	}

	protected boolean getShowMaritalStatus(ServletRequest request) {
		return(true);
	}

	protected boolean getShowRoles(ServletRequest request) {
		return(true);
	}
	
	protected boolean getShowPassword(ServletRequest request) {
		return(true);
	}

	protected boolean getEnterHomeAddress(ServletRequest request) {
		return(true);
	}
    
	protected boolean getEnterWorkAddress(ServletRequest request) {
		return(true);
	}

	protected boolean getShowKnownAs(ServletRequest request) {
		return(true);
	}

    //abstract protected Collection getUserGroupList();
    abstract boolean isUserInGroup(Person person, UserGroup userGroup);

    protected String getContextPath() {
    	return(contextPath);
    }
    
 /*   
	public int doStartTag() throws JspException  {
        log.debug("doStartTag() called");
        
        HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
        contextPath = request.getContextPath();
		Properties params = new Properties();
		Properties invalidParams = new Properties();
        
        Table mainTable = new Table();
        Row mainRow = mainTable.addRow();
        Cell mainCell = mainRow.addCell();

		String userName = userManager.getUser(request).getUserName();     
        
        String userId = userManager.getUser(request).getId();
        
        params.setProperty("user", userId);
    
        User employee = null;
        
        if (userId == null) {
            log.debug("doRender() - new user");
            
            employee = null; 
        }
        else {
            try {
                employee = userManager.findUserById(userId);
                if (employee == null) {
                	log.error("doStartTag() could not find user id = " + userId);
                	throw new JspException("doStartTag() could not find user id = " + userId);
                }
            }
            catch (Exception ex) {
                log.error("doRender() - exception finding user: " + userId, ex);
            }
        }
        
        if (employee == null) {
            log.error("doRender() - user could not be found: " + userId);
            mainCell.addElement("User could not be found: " + userId);
        }
        else {
            if (log.isDebugEnabled()) {
                log.debug("doRender() - employee = " + employee.toString());
            }
            if (invalidParams.size() <= 0) {
                log.debug("doRender() - finding information about userName.");
				params.setProperty("last_name", employee.getLastName());
				params.setProperty("first_name", employee.getFirstName());
				params.setProperty("e_mail", employee.getContactInfo().getEMail());
				params.setProperty("e_mail_2", employee.getContactInfo().getEMail2());
				params.setProperty("work_phone", employee.getContactInfo().getWorkPhone());
				params.setProperty("home_phone", employee.getContactInfo().getHomePhone());
				params.setProperty("password", employee.getPassword());
				params.setProperty("password2", employee.getPassword());
				params.setProperty("cell_phone", employee.getContactInfo().getCellPhone());
            }
            else {
               log.debug("doRender() - request has invalid params, assuming that this form is being shown to fix bad submission.");
            }
            
			SimpleForm form = new SimpleForm(params, invalidParams);
            mainCell.addElement(form);
            form.setAction(getFormAction());
            form.addHidden("action", "save_user");
            form.showInvalidParams();
            
            Table bigTable = new Table();
            form.addElement(bigTable);
            Row row = bigTable.addRow();
            Cell formLeftCell = row.addCell();
            Cell formBlankCell = row.addCell();
            formBlankCell.setWidth(50);
            Cell formRightCell = row.addCell();
            
            Table leftTable = new Table();
            formLeftCell.addElement(leftTable);
            
            Table rightTable = new Table();
            formRightCell.addElement(rightTable);
    
            form.setTable(leftTable);
            form.addSection("User Information", null);

            if (userId == null) {
                form.makeEditRow("user", "User Name", true);
                form.addBlankRow();
            }
            else {
                form.addHidden("user", userId);
                form.makeLabelRow("User", userName);
                form.addBlankRow();
            }

            
            if (showPassword) {
                form.makePasswordEditRow("password", "Password", true);
                form.addBlankRow();
                form.makePasswordEditRow("confirm_password", "Confirm Password", true);
                form.addBlankRow();
            }
            
            form.makeEditRow("first_name", "First Name", true);
            form.addBlankRow();
            form.makeEditRow("last_name", "Last Name", true);
            form.addBlankRow();
            form.makeEditRow("e_mail", "Primary E-Mail", true);
            form.addBlankRow();
            form.makeEditRow("e_mail_2", "Secondary E-Mail", false);
            form.addBlankRow();
            form.makeEditRow("work_phone", "Work Phone", false);
            form.addBlankRow();
            form.makeEditRow("home_phone", "Home Phone", false);
            form.addBlankRow();
            form.makeEditRow("cell_phone", "Mobile Phone", false);
            form.addBlankRow();
    
            if (showRoles) {
                form.setTable(rightTable);
                makeRoleSection(form, employee);
            }
                
            if (showUserGroups) {
                form.setTable(rightTable);
                makeUserGroupSection(form, employee);
            }
            form.addSubmitButton("Submit", "Cancel");
            
        }

		JspWriter writer = pageContext.getOut();
		try {
			writer.print(mainTable.toString());
		}
		catch (IOException ex) {
			throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
		}
		return(EVAL_PAGE);
    }

*/

/*

    private void makeUserGroupSection(SimpleForm form, Person employee) {
        if (roles.size() > 0) { 
            form.addSection("User Groups", null);
            form.addHidden("user_grp_entered", "true");
            Iterator iter = getUserGroupList().iterator();
            while (iter.hasNext()) {
                UserGroup userGroup = (UserGroup)iter.next();
                Input checkBox = new Input();
                checkBox.setType(Input.checkbox);
                String componentName = "GROUP_" + userGroup.getId();
                checkBox.setName(componentName);
                if (isUserInGroup(employee, userGroup)) {
                    checkBox.setChecked(true);
                }
                else {
                    checkBox.setChecked(false);
                }
                form.addInputRow(userGroup.getName(), 
                        componentName, checkBox, false);
            }
        }
    }
*/
    
	/**
	 * @return
	 */
	public boolean getShowPassword() {
		return showPassword;
	}

	/**
	 * @param showPassword
	 */
	public void setShowPassword(boolean showPassword) {
		this.showPassword = showPassword;
	}

	/**
	 * @return
	 */
	public boolean getShowRoles() {
		return showRoles;
	}

	/**
	 * @param showRoles
	 */
	public void setShowRoles(boolean showRoles) {
		this.showRoles = showRoles;
	}

	/**
	 * @return
	 */
	public boolean getShowUserGroups() {
		return showUserGroups;
	}

	/**
	 * @param showUserGroups
	 */
	public void setShowUserGroups(boolean showUserGroups) {
		this.showUserGroups = showUserGroups;
	}

}
